﻿using System.Reflection;
using System.Windows;

namespace JGSY.CourseManagement.Presentation.WpfApp.ViewModels
{
    public class MainViewModel : NotifyPropertyChangedVM
    {
        public UserModel UserInfo { get; set; }

        private string _searchText;

        public string SearchText
        {
            get { return _searchText; }
            set { _searchText = value; SetProperty(ref _searchText,value); }
        }

        private FrameworkElement _mainContent;

        public FrameworkElement MainContent
        {
            get { return _mainContent; }
            set { _mainContent = value; SetProperty(ref _mainContent, value); }
        }

        public DelegateCommand<string> NavChangedCommand { get; set; }

        public MainViewModel()
        {
            UserInfo = new UserModel();
            this.NavChangedCommand = new DelegateCommand<string>(DoNavChanged); 

            DoNavChanged("FirstPageView");
        }

        private void DoNavChanged(object obj)
        {
            //Type type = Type.GetType("JGSY.CourseManagement.Presentation.WpfApp.Views." + obj.ToString());
            //ConstructorInfo cti = type.GetConstructor(System.Type.EmptyTypes);
            //this.MainContent = (FrameworkElement)cti.Invoke(null);
            try
            {
                Type type = Type.GetType("JGSY.CourseManagement.Presentation.WpfApp.Views." + obj.ToString());
                if (type == null)
                {
                    throw new ArgumentException("Type not found.");
                }

                ConstructorInfo cti = type.GetConstructor(System.Type.EmptyTypes);
                if (cti == null)
                {
                    throw new ArgumentException("No parameterless constructor found.");
                }

                MainContent = (FrameworkElement)cti.Invoke(null);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error loading view: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }
}
